/**
 * 文件名：ButtonServiceImpl.java
 *
 * 版本信息：
 * 日期：Aug 21, 2009
 * Copyright dp Corporation 2009 
 * 版权所有 2009All rights reserved.
 * 创建者：
 *
 */
package com.dp.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.ButtonChannel;
import com.dp.pojo.TjChannelaccessVo;

import com.dp.service.impl3.BaseImpl;
import com.dp.util.DataMap;
import com.dp.vo.NewsButtons;
import com.dp.vo.NewsContenttype;
import com.dp.vo.NewsResources;
import com.dp.vo.TChannels;

/**
 * 
 * 项目名称：platform_trunk
 * 类名称：ButtonServiceImpl
 * 类描述：
 * 创建人：Administrator
 * 创建时间：Aug 21, 2009 9:26:36 AM
 * 修改人：Administrator
 * 修改时间：Aug 21, 2009 9:26:36 AM
 * 修改备注：
 * @version 
 * 
 */
@Service
@Transactional
public class ButtonServiceImpl extends BaseImpl{
	
	/*@SuppressWarnings("unchecked")
	public List<ButtonChannel> findAllButton(String buttontxt,String buttontype) {
		String hql = "SELECT new com.dp.pojo.ButtonChannel(nb,nfb,nfc,tc) FROM NewsButtons nb, NewsExtendsFirstchannelButtons nfb," +
		"NewsExtendsFirstchannel nfc,TChannels tc where nb.buttonid = nfb.newsExtendsFirstchannelButtonsPK.buttonid and " +
		"nfb.newsExtendsFirstchannelButtonsPK.channelid = nfc.channelid and nfc.channelId.channelId=tc.channelId";
		return getQueryList(hql);
	}*/
	
	public List<ButtonChannel> findButton(String buttontext,Integer buttontype,int firstrow, int maxrow) {
//		String sql = "SELECT * FROM news_contenttype n left join t_channels t on n.channel_id=t.channel_id";
//		String hql = "SELECT new com.dp.pojo.ButtonChannel(nb,nc,tc) FROM NewsButtons nb, NewsContenttype nc," +
//		"TChannels tc where nb.buttoncontenttype = nc.newscontenttype and nc.channelId = tc.channelId";
//		String hql = "SELECT new com.dp.pojo.ButtonChannel(nb,nc,nfc,tc) FROM NewsButtons nb, NewsContenttype nc," +
//		"NewsExtendsFirstchannel nfc left join nfc.channelId tc where nfc.channelId.channelId=tc.channelId and nb.buttoncontenttype = nc.newscontenttype and nc.channelId = tc.channelId";
//		if (buttontext != null && !"".equals(buttontext)) {
//			hql += " and nb.buttontext like '%" + buttontext + "%'";
//		}
//		if (buttontype != null && buttontype != 0) {
//			hql += " and nb.buttontype = " + buttontype ;
//		}
//		return getQueryList(hql, firstrow, maxrow);
		
		
		 sql = "SELECT nb.buttonid,nb.buttonname,nb.buttontext,nb.buttonicon,nb.buttoncontenttype,nb.buttontype,nb.buttonaction,tc.channel_id,tc.channel_name FROM news_buttons nb,news_contenttype nc left join t_channels tc on nc.channel_id=tc.channel_id " +
				"where nb.buttoncontenttype=newscontenttype";
		if (buttontext != null && !"".equals(buttontext)) {
			sql += " and nb.buttontext like '%" + buttontext + "%'";
		}
		if (buttontype != null && buttontype != 0) {
			sql += " and nb.buttontype = " + buttontype ;
		}
		sql += " order by nb.buttonid desc ";
		List<Object[]> list = this.getNativeQuery(sql, firstrow, maxrow);
		List result = result = new ArrayList<ButtonChannel>();;
		if (list.size() > 0) 
		{
			
			for (Object[] objects : list) 
			{
				if (objects != null && objects.length > 0) 
				{
					int j = 0;
					ButtonChannel bc = new ButtonChannel();
					bc.setButtonid((Integer)objects[j++]);
					bc.setButtonname((String)objects[j++]);
					bc.setButtontext((String)objects[j++]);
					bc.setButtonicon((Integer)objects[j++]);
					bc.setButtoncontenttype((String)objects[j++]);
					bc.setButtontype((Integer)objects[j++]);
					//增加了一个buttonaction字段
					j++;
					bc.setTc(new TChannels((String)objects[j++]));
					bc.setChannelName((String)objects[j++]);
					result.add(bc);
				}
			}
		}
		return result;
	}
	
	public Long findButtonCount(String buttontext,Integer buttontype) {
//		String hql = "SELECT count(nb) FROM NewsButtons nb, NewsContenttype nc , " +
//		"TChannels tc where nb.buttoncontenttype = nc.newscontenttype and nc.channelId = tc.channelId";
//		String hql = "SELECT count(nb) FROM NewsButtons nb, NewsContenttype nc," +
//		"NewsExtendsFirstchannel nfc left join nfc.channelId tc where nfc.channelId.channelId=tc.channelId and nb.buttoncontenttype = nc.newscontenttype and nc.channelId = tc.channelId";
//		if (buttontext != null && !"".equals(buttontext)) {
//			hql += " and nb.buttontext like '%" + buttontext + "%'";
//		}
//		if (buttontype != null && buttontype != 0) {
//			hql += " and nb.buttontype = " + buttontype ;
//		}
//		Object obj = getSingleResult(hql);
//		if(obj ==null ){
//			return Long.valueOf(0+"");
//		}else{
//			return (Long) obj;
//		}
		 sql = "SELECT count(*) FROM news_buttons nb,news_contenttype nc left join t_channels tc on nc.channel_id=tc.channel_id " +
				"where nb.buttoncontenttype=newscontenttype" ;
		if (buttontext != null && !"".equals(buttontext)) {
			sql += " and nb.buttontext like '%" + buttontext + "%'";
		}
		if (buttontype != null && buttontype != 0) {
			sql += " and nb.buttontype = " + buttontype ;
		}
		return (long)(this.getOneInt());
//		List list = this.getNativeQuery(sql);
//		if(list.size()==0 ){
//			return Long.valueOf(0+"");
//		}else{
//			return new Long(list.size());
//		}
	}
	
	public NewsButtons findButtonById(Integer buttonid) {
		String hql = "SELECT nb FROM NewsButtons nb where 1=1 ";
		if (buttonid != null && buttonid != 0) {
			hql += " and nb.buttonid = " + buttonid ;
		}
		Object obj = getSingleResult(hql);
		if(obj == null ){
			return null;
		}else{
			return (NewsButtons) obj;
		}
	}
	//get parent channel by button
	public List getParentChsByBtnid(Integer btnid)
	{
		String sql = "select nc.channelid,nc.newscontenttype from news_extends_firstchannel_buttons nfb,news_contenttype nc " +
				"where nfb.buttonid="+btnid+" and nfb.channelid=nc.channelid";
		List list = this.getNativeQuery(sql);
		return list;
	}
	
	public boolean assingBtnToChannel(Integer btnid,String[] chids)
	{
	
		try {
			ArrayList list =null;// db.queryALLResultSet("select * from news_extends_firstchannel_buttons where buttonid=" +btnid);
			String sql = "delete from news_extends_firstchannel_buttons where buttonid = "+btnid+" and channelid!=" +
		"(SELECT nf.channelid FROM t_channels tc,news_extends_firstchannel nf where tc.channel_name='news' and tc.channel_id=nf.channel_id )";
			ArrayList<String> sqlStrList = new ArrayList<String>();
			sqlStrList.add(sql);
			sql = "";
			if (chids != null) {
				
				//去重
				List ary = new ArrayList(chids.length);
		        for (int i = 0; i < chids.length;) {
		            String ts = chids[i++];
		            if (ary.contains(ts)) continue;
		            ary.add(ts);
		        }
		        chids = new String[ary.size()];
		        for (int i = 0; i < ary.size(); i++) {
		        	chids[i] = (String) ary.get(i);
		        }

				for (String channleidstr:chids) {
					sql = "insert into news_extends_firstchannel_buttons (channelid,buttonid) values ("+channleidstr+","+btnid+")";
					for(int i=0;i<list.size();i++)
					{
						HashMap m = (HashMap)list.get(i);
						int channelid = Integer.parseInt((String)m.get("channelid"));
						String buttonorder = (String)m.get("buttonorder");
						if(Integer.parseInt(channleidstr) == channelid &&(buttonorder != null&&!"".equals(buttonorder.trim())))
						{
							sql = "insert into news_extends_firstchannel_buttons values ("+channleidstr+","+btnid+","+buttonorder+")";
							break;
						}
					}
					sqlStrList.add(sql);
				}
			}
			return true;
	       // return new DBOperate().execTransaction(sqlStrList);
        } catch (Exception e) {
        	log.info(" assingBtnToChannel fail !!!" +e.getMessage());
            return false;
        }
	}
	
	public NewsResources getCurrentIcon(Integer iconid)
	{
		String hql = "";
		if (iconid != null && iconid != 0) {
			hql += " SELECT nr FROM NewsResources nr where  nr.rid = " + iconid ;
			Object obj = getSingleResult(hql);
			if(obj == null ){
				return null;
			}else{
				return (NewsResources) obj;
			}
		}
		else
			return null;
	}
	
	public NewsButtons checkButton(String buttonname,String buttontext,Integer buttonid)
	{
		String hql = "SELECT nb FROM NewsButtons nb where 1=1 ";
		if (buttonname != null && !"".equals(buttonname)) {
			hql += " and nb.buttonname = '" + buttonname + "'";
		}
		if (buttontext != null && !"".equals(buttontext)) {
			hql += " and nb.buttontext ='" + buttontext + "'";
		}
		if (buttonid != null) {
			hql += " and nb.buttonid !=" + buttonid ;
		}
		Object obj = getSingleResult(hql);
		if(obj == null ){
			return null;
		}else{
			return (NewsButtons) obj;
		}
	}
	
//	public boolean update(NewsButtons nb) {
//		String hql = "update NewsContenttype set " ;
//		return getQueryList(hql);
//	}
	
	public List<NewsContenttype> findAllNewsContenttype() {
		String hql = "SELECT nc FROM NewsContenttype nc" ;
		return getQueryList(hql);
	}
	
	 public String getFullChnameById(String fullname,String channelid)
		{
			 sql = "select channel_name,previous_id from t_channels where channel_id='"+channelid+"'";
			try {
//				DataMap result = this.queryOneRow(sql);
				List<Object[]> temp=this.em.createNativeQuery(sql).getResultList();
				if(temp.size()>0)
				{
					String chn=temp.get(0)[0].toString();
					String chnp=temp.get(0)[1].toString();
					fullname =chn + "-" + fullname;
					if(!"news".equals(chn))
					{
						fullname = getFullChnameById(fullname,chnp);
					}
//					fullname = result.getMapData("channel_name")  + "-" + fullname;
//					if(result != null && !result.isEmpty() && !result.getMapData("channel_name").equals("news"))
//					{
//						fullname = getFullChnameById(fullname,result.getMapData("previous_id"));
//					}
				}
				return fullname;
			} catch (Exception e) {
				logger.error(" getFullChnameById fail !!!" +e.getMessage());
				return null;
			}
		}
}
